/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.vng.zing.survey.data;

import static com.vng.zing.survey.data.Base.serialize;
import com.vng.zing.survey.thrift.model.SurveyItem;
import com.vng.zing.survey.thrift.model.Error;
import java.io.IOException;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
import java.util.logging.Level;
import kyotocabinet.DB;
import org.apache.log4j.Logger;

/**
 *
 * @author kienpd
 */
public class SurveyData extends Base {
    private static final Logger logger_ = Logger.getLogger(SurveyData.class);
    private static final Lock createLock_ = new ReentrantLock();
    private static SurveyData _instance = null;
    private final DB db;

    public SurveyData() {
        db = new DB();
        if (!db.open("database/survey.kch"+cofigdata , DB.OWRITER | DB.OCREATE|DB.OAUTOTRAN)) {
            System.err.println("open error: " + db.error());
        }

    }

    public static SurveyData getInstance() {
        if (_instance == null) {
            createLock_.lock();
            try {
                if (_instance == null) {
                    _instance = new SurveyData();
                }
            } finally {
                createLock_.unlock();
            }
        }
        return _instance;
    }

    public Error setSurvey(int idSurvey, SurveyItem surveyItem) {
        try {
            byte[] key = serialize(idSurvey);
            byte[] value = serialize(surveyItem);
            if (!db.set(key, value)) {
                return Error.ERR_SYSTEM;


            }
            return Error.SUCCESS;
        } catch (IOException ex) {
            logger_.error("SurveyData.setSurvey:"+ex.getMessage(), ex);
        }
        return Error.ERR_SYSTEM;
    }

    public SurveyItem getSurvey(int idSurvey){

        try {
            byte[] key = serialize(idSurvey);
            byte[] value = db.get(key);
            if (value == null) {

                return null;
            }
            SurveyItem surveyItem = new SurveyItem();
            surveyItem = (SurveyItem) deserialize(value);
            return surveyItem;
        } catch (IOException  ex) {
             logger_.error("SurveyData.getSurvey:"+ex.getMessage(), ex);
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(SurveyData.class.getName()).log(Level.SEVERE, null, ex);
        }
        return null;

    }
    public Error removeSurvey(int idSurvey){
        try {
            byte[] key = serialize(idSurvey);
            if(db.seize(key) == null){
             return Error.IDSURVEY_NO_EXITS;
            }
            return Error.SUCCESS;
        } catch (IOException ex) {
           logger_.error("SurveyData.removeSurvey:"+ex.getMessage(), ex);
        }
        return Error.ERR_SYSTEM;
    }
     public Error replaceSurvey(int idSurvey, SurveyItem surveyItem) {
        try {
            byte[] key = serialize(idSurvey);
            byte[] value = serialize(surveyItem);
            if (!db.replace(key, value)) {
                return Error.ERR_SYSTEM;
            }
            return Error.SUCCESS;
        } catch (IOException ex) {
            logger_.error("SurveyData.replaceSurvey:"+ex.getMessage(), ex);
        }
        return Error.ERR_SYSTEM;
    }
    
}
